<?php
namespace app\admin\model;

use think\Model;
class Access extends Model
{


	public function selectAll()
	{
		$select_all = $this->select();
		return $select_all;
	}
	 
	 public function listorder($ids=0)
	 {
		  $pk = $this->getPk();
		  foreach ($ids as $key => $r) {
            $data['listorder'] = $r;
            $this->where(array($pk => $key))->save($data);
        }
		 return true;
	 }
	 
	

	

	



	
    public function getList($options = array()){
        if(is_numeric($options)){
            $parent_id 	= $options;
            $options 	= array();
        }else{
            $options["parent_id"] = isset($options["parent_id"]) 	? $options["parent_id"] : null;
            $parent_id            = is_null($options["parent_id"]) 	? $this->parent_id 	: $options["parent_id"];
        }
	    $auth = new \org\Auth();
		$info = $auth->getGroups(session('id'));
		$access_id = M('authGroup')->where('id='.$info[0]['group_id'])->find();
		$where['id']=array('in',$access_id['listrules']);
		$where['parent_id']=$parent_id;
        return $this->where($where)->order('listorder desc')->select($options);
    }
	
	

	public function hasChild($options){
		$parent_id = $options;
        $rs = $this->where(["parent_id"=>$parent_id])->order('listorder desc')->select();
		return empty($rs) ? false : $rs;
	}

	public function getChilds($options){
		return $this->_getChilds($options);
	}

	public $childNode = array();

	private function _getChilds($id){
		$child = $this->where(["parent_id"=>$id])->order('listorder desc')->select();
		if($child){
			foreach ($child as $key => $value) {
				$this->childNode[] = $value["id"];
				$this->_getChilds($value["id"]);
			}
		}
		return $this->childNode;
	}
    public function depth($options){
        return $this->_depth($options);
    }
    public $depth = ['0'=>0];

    private function _depth($parent_id){
        $rs = D("Term")->where(['id'=>$parent_id])->find();
        array_push($this->depth, $parent_id);
        if($rs['parent_id'] != 0){
            $this->_depth($rs['parent_id']);
        }
        return $this->depth;
    }
}
